/**
 * Copyright (C) 2007 The AsyncMail Group <asyncmail@googlegroups.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.asyncmail.smtpserver.handler.fastfail;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Required;

import com.googlecode.asyncmail.common.DSNStatus;
import com.googlecode.asyncmail.common.MailAddress;
import com.googlecode.asyncmail.dnsservice.DNSService;
import com.googlecode.asyncmail.dnsservice.TemporaryResolutionException;
import com.googlecode.asyncmail.smtp.SMTPRetCode;
import com.googlecode.asyncmail.smtpserver.SMTPSession;
import com.googlecode.asyncmail.smtpserver.hook.HookResult;
import com.googlecode.asyncmail.smtpserver.hook.HookReturnCode;
import com.googlecode.asyncmail.smtpserver.hook.MailHook;

/**
 * Add MFDNSCheck feature to SMTPServer. This handler reject mail from domains
 * which have not an an valid MX record.
 * 
 */
public class ValidSenderDomainHandler implements MailHook {

    private DNSService dnsServer = null;

    private Log logger;

    @Required
    public void setLogger(Log logger) {
        this.logger = logger;
    }

    public Log getLogger() {
        return logger;
    }

    @Required
    public void setDnsService(DNSService dnsServer) {
        this.dnsServer = dnsServer;
    }

    protected boolean check(SMTPSession session, MailAddress senderAddress) {
        // null sender so return
        if (senderAddress == null)
            return false;

        Collection<String> records = null;

        // try to resolv the provided domain in the senderaddress. If it can not
        // resolved do not accept it.
        try {
            records = dnsServer.findMXRecords(senderAddress.getDomainPart());
        } catch (TemporaryResolutionException e) {
            // TODO: Should we reject temporary ?
        }

        if (records == null || records.size() == 0) {
            return true;
        }

        return false;
    }

    /**
     * @see com.googlecode.asyncmail.smtpserver.hook.MailHook#doMail(com.googlecode.asyncmail.smtpserver.SMTPSession,
     *      org.apache.mailet.MailAddress)
     */
    public HookResult doMail(SMTPSession session, MailAddress sender) {
        if (check(session, sender)) {
            return new HookResult(HookReturnCode.DENY,
                    SMTPRetCode.SYNTAX_ERROR_ARGUMENTS, DSNStatus.getStatus(
                            DSNStatus.PERMANENT,
                            DSNStatus.ADDRESS_SYNTAX_SENDER)
                            + " sender "
                            + sender
                            + " contains a domain with no valid MX records");
        } else {
            return new HookResult(HookReturnCode.DECLINED);
        }
    }
}
